package cn.mysource.org.controller;

import cn.mysource.basic.config.BussinessException;
import cn.mysource.basic.util.AjaxResult;
import cn.mysource.basic.util.PageList;
import cn.mysource.org.domain.Department;
import cn.mysource.org.domain.Permission;
import cn.mysource.org.query.DepartmentQuery;
import cn.mysource.org.query.PermissionQuery;
import cn.mysource.org.service.IDepartmentService;
import cn.mysource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;



    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询详情" )
    public Department findById(@ApiParam(value="部门id",required = true) @PathVariable("id") Long id) {
        return departmentService.findById(id);

    }


    @GetMapping()
    public List<Department> findAll() {
        return departmentService.findAll();
    }


    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if (null != department.getId()) {
            // 修改
            departmentService.update(department);
        } else {
            // 添加
            departmentService.add(department);
        }
        return AjaxResult.me().success();
    }


    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

   //分页查询
   @PostMapping()
   public PageList<Department> queryByPage(@RequestBody PermissionQuery query) {
       return departmentService.queryByPage(query);
   }
    //批量删除
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        departmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }
    @GetMapping("/tree")
    public List<Department> tree() {
        return departmentService.tree();
    }

}
